/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bookingsystem;

import java.io.IOException;
import java.io.InterruptedIOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.SocketException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author admin
 */
public class CheckConnectionThread implements Runnable {
    private Socket client = null;
    private ObjectOutputStream oos = null;
    private ObjectInputStream ois = null;
    
    public CheckConnectionThread(Socket client, ObjectOutputStream oos, ObjectInputStream ois) {
        this.client = client;
        try {
            this.client.setSoTimeout(500);
        } catch (SocketException ex) {
            Logger.getLogger(CheckConnectionThread.class.getName()).log(Level.SEVERE, null, ex);
        }
        this.oos = oos;
        this.ois = ois;
    }
    
    public synchronized void run() {
        Object isAlive = null;
        while (true){
            
            try {

                oos.writeObject("areYouAlive");
                oos.flush();
                isAlive = ois.readObject();
                this.wait(4000);
            } catch (InterruptedIOException e){
                System.err.println("Remote host timed out during read operation");
                System.exit(1);
            } catch (IOException e){
                System.out.println(e);
                //JOptionPane.showMessageDialog(null, "Server is lost", "Connection Error", 0);
                Object[] options = { "CLOSE" };
                JOptionPane.showOptionDialog(null, "Server is closed", "Connection Error", JOptionPane.DEFAULT_OPTION, JOptionPane.ERROR_MESSAGE,null, options, options[0]);
                System.exit(1);
            } catch (ClassNotFoundException e){
                System.out.println(e);
            } catch (InterruptedException e){
                System.out.println(e);
            }
        }
    }
}
